<template>
  <div class="level">
    <h1>医院</h1>
    <div class="content">
      <div class="left">等级：</div>
      <ul class="hospital">
        <li :class="{ active: activeFlag == '' }" @click="changeLevel('')">全部</li>
        <li v-for="level in levelArr" :key="level.value" :class="{ active: activeFlag == level.value }"
          @click="changeLevel(level.value)">{{ level.name }}
        </li>
      </ul>
    </div>
  </div>
</template>

<script setup lang="ts" name="Level">
//@ts-ignore
import { reqHospitalLevelAndRegion } from '@/api/home/index'
//@ts-ignore
import type { HospitalLevelAndRegionResponseData, HospitalLevelAndRegionArr } from '@/api/home/type'
import { onMounted, ref } from 'vue';

//存储医院等级数据的数组
let levelArr = ref<HospitalLevelAndRegionArr>([])
//控制医院等级高亮的数据
let activeFlag = ref<string>('')

//获取医院等级数据
const getLevel = async () => {
  let result: HospitalLevelAndRegionResponseData = await reqHospitalLevelAndRegion('HosType')
  if (result.code == 200) {
    levelArr.value = result.data
  }
}
//组件挂载完毕：发送请求
onMounted(() => {
  getLevel()
})
//点击医院等级的回调：等级高亮，（声明）触发自定义事件
//声明自定义事件
let $emit = defineEmits(['getLevel'])
const changeLevel = (level: string) => {
  activeFlag.value = level
  //触发自定义事件：将医院等级参数回传给父组件
  $emit('getLevel', level)
}
</script>

<style scoped lang="scss">
.level {
  color: #7f7f7f;

  h1 {
    font-weight: 900;
    margin: 10px 0;
  }

  .content {
    display: flex;

    .left {
      margin-right: 10px;
      // width: 40px;
    }

    .hospital {
      display: flex;

      li {
        margin-right: 10px;

        &.active {
          color: #55a6fe;
        }
      }

      li:hover {
        color: #55a6fe;
        cursor: pointer;
      }
    }
  }
}
</style>